#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#define MAX1 1024*4
#define MAX2 512
#define LAENGE 10
#define M 10

int nl(int argc, char *argv[])
{
int s_op=0,count_lines=1,contor;
int i,j,total_fisiere=0,fd;
char array[M][1024];
char t[20],linie[256];
int var;
char *buff = (char *)(sizeof(char)*1024);
char *x;
i=1;
	while(i<argc)
		{
			if(strcmp(argv[i],"-s") == 0)
				{
					s_op=1;
					i++;
					strcpy(t,argv[i]);
				}
			else {
					strcpy(array[total_fisiere],argv[i]);
					total_fisiere++;
					}
		i++;
		}
i=0;
while(i<total_fisiere)
	{
		contor = 0;
		fd = open(array[i],O_RDONLY);
		if(fd<0)
			{
			printf("Could not read the file!\n");
			continue;
			}
		while(var= read (fd,linie,256) > 0)
			{
				x = strtok (linie,"\n");
				fprintf(stdout, "\t%2d",count_lines);
				fprintf(stdout, "\t%s",t);
				fprintf(stdout, "\t%s\n",linie);
				count_lines++;
				while ((x = strtok (NULL,"\n")) != NULL)
					{
					var= read (fd,linie,256);
					fprintf(stdout, "\t%2d",count_lines);
					fprintf(stdout, "\t%s",t);
					fprintf(stdout, "\t%s\n",linie);
					count_lines++;
					}
			}
		printf("\n");
		i++;
	}
}


int head(int argc, char*argv[])
{
	int nop=0, cop=0, qop=0, vop=0;
	int nr=10,i,j,total_fisiere=0,fd,var,contor=0;
	char array[M][1024];
	char *buff = (char *)malloc(sizeof(char)*1024);

i=1;
	while( i<argc)
	{
		if(strcmp(argv[i],"-c") == 0)

		{
			cop = 1;
			i++;
			nr = atoi(argv[i]);
		}
		else if(strcmp(argv[i],"-n") == 0)
		{
			nop = 1;
			i++;
			nr = atoi(argv[i]);
		}
		else if(strcmp(argv[i],"-q") == 0)
			qop = 1;
		else if(strcmp(argv[i],"-v") == 0)
			vop = 1;
		else{
			strcpy(array[total_fisiere],argv[i]);
			total_fisiere++;
		}
i++
	}
i=0
	while( i< total_fisiere)
	{
		contor=0;// cand trece la urm fisier il reseteaza
		fd = open(array[i],O_RDONLY);
		 if(fd < 0)
		{
			printf("Could not read file!");
			continue;
		}

		if((vop == 1 || total_fisiere>1) && ( qop == 0))
		{
			printf("\n==> %s <==\n",array[i]);
		}

		while((var=read(fd,buff,1023)) > 0 )
		{
			for(j=0; j< var; j++)
			{
				if(contor >= nr)
				break;

				if( cop == 1 )
					contor++;
				else if (buff[j] == '\n')
					contor++;

				printf("%c",buff[j]);
			}
		}

		printf("\n");
i++;
	}


}
int cmd_chroot(int argc, char *argv[])
{
int i;
 if(strcmp(argv[1],NULL) != 0)
chroot(argv[i]);
else printf("Command error");	
}


int cmd_help(int argc, char *argv[])
{
    int i;

    if(strcmp(argv[1],"nl") == 0)
        printf("nl command numbers lines in a file.\n -s Separator\t 	 Separates the text from its line number with the character specified in the Separator variable. The default value of the Separator variable is a tab character.\n");
    else if(strcmp(argv[1],"head") == 0)
        printf("Head command displays the first ten lines of a file, unless otherwise stated.\n -c Number\tSpecifies the number of bytes to display. The Number variable must be a positive decimal integer.\n-n Number\tSpecifies the number of lines from the beginning of each specified file to be displayed. The number variable must be a positive decimal integer. This flag is equivalent to the -Count flag.\n-q  Quiet\tNever print headers giving file names\n-v verbose\talways print headers giving file names\n");
	else if(strcmp(argv[1],"chroot") == 0)
		printf("Changes the root directory of a command.\n");
	else printf("Command not found!\n");
}

void helpt(){
printf("Terminal 1.01\n\n");
printf("Implemented commands:\n");
printf("  nl -s \n");
printf("  head -c -n -v -q\n");
printf("  chroot\n");

}
void version()
{
printf("\t############################################\n");
printf("\t#                                          #\n");
printf("\t#      Author: Fratutu Stefan Madalin      #\n");
printf("\t#                                          #\n");
printf("\t#      Version: terminal1.01               #\n");
printf("\t#                                          #\n");
printf("\t#      © all rights reserved!              #\n");
printf("\t############################################\n");

}


int main()
{
	char *cmd=(char*)malloc(sizeof(char)*MAX1);
	char **argv=(char**)malloc(sizeof(char*)*MAX2);
	int argc=0, i;
	char *s;


printf("\t############################################\n");
printf("\t#                                          #\n");
printf("\t#           Fratutu Stefan Madalin         #\n");
printf("\t#                                          #\n");
printf("\t#              Assignment I                #\n");
printf("\t#                                          #\n");
printf("\t#             Operating Systems            #\n");
printf("\t############################################\n");

	while((strcmp(cmd,"exit")) != 0)
	{
		
		printf("$$$$-> ");

		fgets(cmd,MAX1,stdin);
		cmd[strlen(cmd)-1]='\0';

		s=strtok(cmd," ");
		argc=0;
		argc++;
		while(s != NULL)
		{
			argv[argc-1] = (char*)malloc(sizeof(char)*MAX1);
			strcpy(argv[argc-1] , s);
			
			s=strtok(NULL , " ");
			if (s != NULL)
				argc++;
		}

		if((strcmp(argv[0] , "nl")) == 0)
			nl(argc , argv);

		if (strcmp(argv[0],"head") == 0)
			head(argc, argv);
		if (strcmp(argv[0],"chroot") == 0)
			cmd_chroot(argc, argv);

		if(strcmp(argv[0],"help") == 0)
			cmd_help(argc, argv);

		if(strcmp(argv[0],"version") == 0)
			version();
		
		if(strcmp(argv[0],"helpt") == 0)
			helpt();
		
		


	}
	free(cmd);
	return 0;



}





